import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { _HttpClient, ModalHelper, SettingsService } from '@delon/theme';
import { STColumn, STComponent, STColumnBadge, STColumnTag, STData } from '@delon/abc';
import { SFSchema, SFComponent } from '@delon/form';
import { SystemDictEditComponent } from './dictedit/dictedit.component';
import { NzNotificationService } from 'ng-zorro-antd';
import { Router } from '@angular/router';
import { RouteService } from '@core';

const BADGE_DICTTYPE: STColumnTag = {
  '0': { text: '业务类', color: 'blue' },
  '1': { text: '系统类', color: 'red' },
};

/**
 * 字典管理
 */
@Component({
  selector: 'app-system-sysdict',
  templateUrl: './sysdict.component.html',
})
export class SystemDictComponent implements OnInit, AfterViewInit {
  auth_btn_add = false;
  auth_btn_edit = false;
  auth_btn_del = false;
  pi = 1;
  url = `/cf/admin/dict/page`;
  params = { type: '' };

  @ViewChild('st', { static: false }) st: STComponent;
  columns: STColumn[] = [
    { title: '字典key', index: 'type' },
    { title: '描述', index: 'description' },
    {
      title: '字典类型',
      index: 'system',
      type: 'tag',
      tag: BADGE_DICTTYPE,
      filter: {
        menus: [{ text: '系统类', value: '1' }, { text: '业务类', value: '0' }],
        multiple: false,
      },
    },
    { title: '备注', index: 'remarks' },
    { title: '创建日期', index: 'createTime', type: 'date' },
    {
      title: '操作',
      buttons: [
        {
          text: '字典项',
          type: 'link',
          click: (record: STData, modal?: any, instance?: STComponent) => {
            this.router.navigateByUrl(`/system/dict/detail/${record.id}`, {
              state: { pi: this.st.pi, type: this.params.type },
            });
          },
        },
        {
          text: '编辑',
          type: 'static',
          modal: { component: SystemDictEditComponent, paramsName: 'record' },
          click: 'reload',
          iif: (item: STColumn) => {
            return this.auth_btn_edit && item.system !== '1';
          },
        },
        {
          text: '删除',
          type: 'del',
          pop: {
            title: '确定删除字典？',
            okType: 'danger',
          },
          click: (record, _modal, comp) => {
            this.http.delete(`/cf/admin/dict/${record.id}`).subscribe(res => {
              if (res.status === 200) {
                this.notice.success('成功', `成功删除【${record.type}】`);
                comp!.load();
              }
            });
          },
          iif: (item: STColumn) => {
            return this.auth_btn_del && item.system !== '1';
          },
        },
      ],
    },
  ];

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    private notice: NzNotificationService,
    private settings: SettingsService,
    private router: Router,
    private routeStateSrv: RouteService,
  ) {
    this.pi = this.routeStateSrv.pi;
    this.params = { type: this.routeStateSrv.get('type') || '' };
  }

  ngAfterViewInit(): void {}

  ngOnInit() {
    this.auth_btn_add = this.settings.user.permissions.sys_dict_add;
    this.auth_btn_edit = this.settings.user.permissions.sys_dict_edit;
    this.auth_btn_del = this.settings.user.permissions.sys_dict_del;
  }

  add() {
    this.modal.createStatic(SystemDictEditComponent, { i: { id: 0 } }).subscribe(() => this.st.reload());
  }

  reset() {
    this.params = { type: '' };
    this.st.load(1, this.params);
  }
}
